home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 45975 / 45975.xpi / content / dicservices.js < prev    next >
Text File  |  2009-11-23  |  5KB  |  103 lines

  1. /*
  2.  * Copyright (c) 2009 Bui Viet Thanh (thanhbv@gmail.com).
  3.  *
  4.  * This file is part of clicknlearn.
  5.  *
  6.  * clicknlearn is free software: you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation, either version 3 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * clicknlearn is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with clicknlearn.  If not, see <http://www.gnu.org/licenses/>.
  18.  */
  19.  
  20. //Components.utils.import("resource://weave/log4moz.js");
  21.  
  22. /**
  23.  * Represent a Dictionary source (service)
  24.  * @param name - name of the DicService. ex: "tratu.vn"
  25.  * @param baseUrl - ex, "http://tratu.vn/dispatchaddon.php?dict=en_vn&title="
  26.  *      the request url will be (baseUrl + word) 
  27.  * @param notfoundPattern - if(response.indexOf(this.notfoundPattern) >= 0)
  28.  *      then we consider that the word was not found.
  29.  *      notfoundPattern can contains the string "${word} to represent the looking up word
  30.  * @param selectors (optinal) - we don't show the whole response.
  31.  *      Ex, with HoNgocDucDicService: selectors = "#result".
  32.  *          => only show the element with id="result" in the responce html
  33.  *      see: https://developer.mozilla.org/En/DOM/Locating_DOM_elements_using_selectors
  34.  *      we can use firebug, or "Adblock Plus: Element Hiding Helper" to find selectors
  35.  *      if selectors is not specified
  36.  */
  37. function DicService(name, baseUrl, notfoundPattern, selectors){
  38.     this.name = name;
  39.     this.baseUrl = baseUrl;
  40.     this.notfoundPattern = notfoundPattern;
  41.     this.selectors = typeof selectors != 'undefined' ? selectors : null;
  42. }
  43. DicService.prototype.getUrl = function(word) {
  44.     return this.baseUrl + encodeURIComponent(word);
  45. }
  46. DicService.prototype.parseAndPrint = function(response) {
  47.     response = this.parse(response);
  48.  
  49.     var html = CNLUtils.getString(
  50.             "<label for='cnlwordbox'>${lookup} </label>" +
  51.             "<input type='text' id='cnlwordbox' maxlength='60' size='15' value='%%' />" +
  52.             "<button id='cnl_settingsButton' class='clicknlearnRightButton'><img src='chrome://clicknlearn/skin/settings24.png' /></button>" +
  53.             "<br /><label for='remindbox'>${remind} </label>" +
  54.             "<input type='text' id='remindbox' maxlength='256' size='70' value='%%' />" +
  55.             "<br /><button class='clicknlearnButton' id='",
  56.             [CNL.DICLOOKUP.currentWord, CNL.DICLOOKUP.currentRemind]);
  57.     if(CNL.DICLOOKUP.remember)
  58.         html += CNLUtils.replaceAntStyleString(
  59.                 "removeWordButton'>${knewByHeart}</button><br />");
  60.     else
  61.         html += CNLUtils.replaceAntStyleString(
  62.                 "remindButton'>${addNewWord}</button><br />");
  63.     html += response;
  64.  
  65.     CNL.DICLOOKUP.div.innerHTML = html;
  66.     var textbox = CNL.DICLOOKUP.div.firstChild.nextSibling;
  67.     textbox.focus();
  68. }
  69. DicService.prototype.parse = function(response){
  70.     if(! this.selectors)
  71.         return response;
  72.     var d = CNL.currentDoc.createElement('div');
  73.     d.innerHTML = response;
  74.     var resultDiv = d.querySelector(this.selectors);
  75.     return resultDiv.innerHTML;
  76. }
  77.  
  78. DicService.prototype.wordNotFound = function(response){
  79. //    let notfoundStr = this.notfoundPattern.replace("${word}", CNL.DICLOOKUP.currentWord);
  80.     return response.indexOf(this.notfoundPattern) >= 0;
  81. }
  82. ///////////////////////////////////////////////////////////////////////////////////////////////////
  83.  
  84. //function HoNgocDucDicService(){
  85. //    this.baseUrl = "http://www.informatik.uni-leipzig.de/~duc/TD/td/index.php?db=ev&word=";
  86. //    this.name = "Hß╗ô Ngß╗ìc ─Éß╗⌐c";//CNL.stringsBundle.getString('hoNgocDuc');
  87. //    this.selectors = "#result";
  88. //}
  89. //HoNgocDucDicService.prototype = new DicService();
  90. //
  91. //HoNgocDucDicService.prototype.wordNotFound = function(response){
  92. //    var s = '<div id="result" onkeyup="lookupSelected();"> <font size=+1 color=#FF0000>';
  93. //    var i1 = response.indexOf(s) + s.length;
  94. //    var i2 = response.indexOf('<', i1);
  95. //    var returnWord = response.substring(i1, i2);
  96. //    return returnWord != CNL.DICLOOKUP.currentWord;
  97. //}
  98. ///////////////////////////////////////////////////////////////////////////////////////////////////
  99.  
  100. //let logger = Log4Moz.repository.getLogger("CNL.Dicservice");
  101. //logger.level = Log4Moz.Level["All"];
  102. //logger.debug("Hß╗ô Ngß╗ìc ─Éß╗⌐c!! " +   CNL_DIC_SERVICES[4].name);
  103.